﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using GeneralLibs;
using System.Data.SqlClient;

namespace BasicForms
{
    public partial class FrmProgressBarDev : Form
    {
        public FrmProgressBarDev()
        {
            InitializeComponent();

            progressPanel.LookAndFeel.SkinName = "Office 2007 Blue";
        }

        private void FrmProgressBarControl_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            timerCheckClose.Start();
            //this.Height = 16;
        }

        delegate void SetShowDialogCallBack(FrmProgressBarDev form);

        private static void ShowForm(FrmProgressBarDev form)
        {
            form.ShowDialog();
        }

        private static void ShowProgressForm()
        {
            FrmProgressBarDev form = new FrmProgressBarDev();
            if (form.InvokeRequired)
            {
                SetShowDialogCallBack safe = new SetShowDialogCallBack(ShowForm);
                form.ParentForm.Invoke(safe, new object[] { form });
            }
            else
            {
                form.ShowDialog();
            }
        }

        private static bool _ShowProgressBar = false;
        public static bool ShowProgressBar
        {
            get
            {
                return _ShowProgressBar;
            }
        }

        private static string _Status = "";
        public static string Status
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
            }
        }

        private static Thread threadShowProgress;

        public static void StartShowProgress(string status)
        {
            _Status = status;
            StartShowProgress();
        }
        public static void StartShowProgress()
        {
            try
            {
                _ShowProgressBar = true;
                threadShowProgress = new Thread(new ThreadStart(ShowProgressForm));
                threadShowProgress.Start();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static void FinishShowProgress()
        {
            _Status = "";
            _ShowProgressBar = false;
        }

        private void timerCheckClose_Tick(object sender, EventArgs e)
        {
            try
            {
                if (!_ShowProgressBar)
                {
                    this.Close();
                }
                else
                {
                    progressPanel.Description = _Status;
                    if (_Status == "")
                    {
                        //this.Height = 45;
                    }
                    else 
                    {
                        //progressPanel.Caption = "";
                        if (_Status.Length < 20)
                        {
                            //this.Height = 45;
                            this.Width = 150;
                        }
                        else if (_Status.Length < 30)
                        {
                            //this.Height = 45;
                            this.Width = 200;
                        }
                        else
                        {
                            //this.Height = 45;
                            this.Width = 350;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
    }
}
